package service

import (
	"elegantGo/chapter17/oo"
)

type UserArticlePermission struct {
	sessionUser oo.User
	ooArticle   oo.Article
}

func NewUserArticlePermission(sessionUser oo.User, ooArticle oo.Article) *UserArticlePermission {
	srv := new(UserArticlePermission)
	srv.sessionUser = sessionUser
	srv.ooArticle = ooArticle
	return srv
}

func (srv *UserArticlePermission) Readable() bool {
	if srv.sessionUser == nil {
		return false
	}

	if _, ok := srv.ooArticle.(*oo.FreeArticle); ok {
		return true
	}

	if srv.ooArticle.GetUser().IsAuthor(srv.sessionUser) {
		return true
	}

	if _, ok := srv.ooArticle.(*oo.VipArticle); ok && srv.sessionUser.IsVip() {
		return true
	}

	if payPerArticle, ok := srv.ooArticle.(*oo.PaidArticle); ok {
		return payPerArticle.IsPaid(srv.sessionUser)
	}

	if srv.sessionUser.IsAdministrator() {
		return true
	}

	return false
}
